package cn.stronglink.asset.module.basicdata.storeRoom.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.StoreRoom;
import cn.stronglink.asset.module.basicdata.storeRoom.service.StoreRoomService;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "basic/storeroom")
public class StoreRoomController extends AbstractController {

	@Autowired
	private StoreRoomService storeRoomService;

	@RequestMapping(value = "queryList")
	public ResponseEntity<ModelMap> queryList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		@SuppressWarnings("unchecked")
		Page<StoreRoom> page = (Page<StoreRoom>) super.getPage(params);
		Page<StoreRoom> data = storeRoomService.queryStoreRoom(page, params);
		return setSuccessModelMap(modelMap, data);
	}
	
	@RequestMapping(value = "queryRoomList")
	public ResponseEntity<ModelMap> queryRoomList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {		
		List<StoreRoom> data = storeRoomService.queryRoomList();
		return setSuccessModelMap(modelMap, data);
	}
	
	@OperateLog(module = "库房管理",desc="添加库房信息", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody StoreRoom storeRoom) {
		storeRoomService.insertStoreRoom(storeRoom);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "库房管理",desc="修改库房信息", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody StoreRoom storeRoom) {
		storeRoomService.insertStoreRoom(storeRoom);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "库房管理",desc="删除库房信息", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		int isUsed =storeRoomService.getIsUsedRoom(id);
		if(isUsed > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("IsUsedStoreRoom"));
		}
		storeRoomService.removeStoreRoom(id);
		return setSuccessModelMap(modelMap, null);
	}

}
